<?php

class Product extends Admin_Controller {

    function Product() {
        parent::Admin_Controller();
        $this->load->model('Product_m');
    }

    function index() {
        $this->list_animaux();
    }

    function list_animaux() {
        is_admin();
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 7;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/product/list_animaux/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;

        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $config['total_rows'] = $this->db->count_all('animaux');
        if ($config['total_rows'] == $offset && $offset > 0) {
            $offset = $offset - $per_page;
        }
        $this->_data['query'] = $this->Product_m->get_all_animals($config['per_page'], $offset);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['per_page'] = $config['per_page'];
        $this->_data['total'] = $this->db->count_all('animaux');
        $this->_data['off_set'] = $offset;
        $this->_data['page_title'] = "Liste de Animaus";
        $this->display_view('product/animaux');
    }

    function add_new_animal() {
        is_admin();
        $this->_data['animal_price'] = $this->input->post('txtPrice');
        $this->_data['page_title'] = "Ajouter de nouveaux animaux";
        $config = array(
            array(
                'field' => 'txtAnimalName',
                'label' => 'Name',
                'rules' => 'required'
            ),
            array(
                'field' => 'txtQuantity',
                'label' => 'Quantity',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            $this->_data['selected_country'] = $this->input->post('origine');
            $this->_data['selected_cat'] = $this->input->post('categorie');
            if ($this->form_validation->run()) {
                $image_path = './assets/productimg';
                $thumb_path = './assets/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000
                );
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading ...";
                if (!$this->upload->do_upload()) {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->display_view('product/add_new_animal');
                } else {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nom' => $this->input->post('txtAnimalName'),
                        'catid' => $this->input->post('categorie'),
                        'origine' => $this->input->post('origine'),
                        'prix' => $this->input->post('txtPrice'),
                        'quantite' => $this->input->post('txtQuantity'),
                        'img' => $upload_info['file_name'],
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Product_m->add_animal($data);
                    redirect(site_url('product/list_animaux'));
                    //print_r($data);
                }
            } else {
                $this->display_view('product/add_new_animal');
            }
        } else {
            $this->display_view('product/add_new_animal');
        }
    }

    function delete_animal($id, $per_page, $off_set) {
        $this->Product_m->delete_animal($id);
        redirect('product/list_animaux' . '/' . $per_page . '/' . $off_set);
    }

    function update_animal($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Mise à jour de nouvel animal";
        $config = array(
            array(
                'field' => 'txtAnimalName',
                'label' => 'Name',
                'rules' => 'required'
            ),
            array(
                'field' => 'txtQuantity',
                'label' => 'Quantity',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->_data['animals'] = $this->Product_m->get_animal_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/productimg';
                $thumb_path = './assets/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000
                );
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading...";
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'nom' => $this->input->post('txtAnimalName'),
                        'catid' => $this->input->post('categorie'),
                        'origine' => $this->input->post('origine'),
                        'prix' => $this->input->post('txtPrice'),
                        'quantite' => $this->input->post('txtQuantity'),
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Product_m->update_animal($data, $id);
                    redirect('product/list_animaux' . '/' . $per_page . '/' . $off_set);
                } else {
                    $query = $this->Product_m->get_animal_by_id($id);
                    if ($query->num_rows() > 0) {
                        $info = $query->row();
                        $image_to_delete = './assets/productimg/' . $info->img;
                        $thumb_to_delete = './assets/thumbs/' . $info->img;
                        delete_image($image_to_delete, $thumb_to_delete);
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nom' => $this->input->post('txtAnimalName'),
                        'catid' => $this->input->post('categorie'),
                        'origine' => $this->input->post('origine'),
                        'prix' => $this->input->post('txtPrice'),
                        'quantite' => $this->input->post('txtQuantity'),
                        'img' => $upload_info['file_name'],
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Product_m->update_animal($data, $id);
                    redirect('product/list_animaux' . '/' . $per_page . '/' . $off_set);
                }
            } else {
                $this->display_view('product/update_animal');
            }
        } else {
            $this->display_view('product/update_animal');
        }
    }

    function liste_categorie() {
        is_admin();
        $this->_data['page_title'] = "Liste de categorie";
        $per_page = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 4;
        $off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
        $config['base_url'] = base_url() . 'index.php/product/liste_categorie/' . $per_page . '/';
        $config['per_page'] = '4';
        $config['uri_segment'] = 4;
        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        $config['total_rows'] = $this->db->count_all('categories');
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Product_m->get_product_categories($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['per_page'] = $config['per_page'];
        $this->_data['off_set'] = $off_set;
        $this->display_view('product/animaux_categorie');
    }

    function add_animal_category() {
        is_admin();
        $this->_data['page_title'] = "Ajouter une catégorie d'animaux";
        $config = array(
            array(
                'field' => 'fr_cat_name',
                'label' => 'Nom',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/catimg';
                $thumb_path = './assets/catimg/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 4000
                );
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading...";
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'nomfr' => $this->input->post('fr_cat_name'),
                        'nomen' => $this->input->post('en_cat_name'),
                        'nomes' => $this->input->post('spa_cat_name'),
                        'nomal' => $this->input->post('ger_cat_name'),
                        'nomho' => $this->input->post('du_cat_name'),
                        'parent' => $this->input->post('parent')
                    );
                    $this->Product_m->add_category($data);
                } else {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nomfr' => $this->input->post('fr_cat_name'),
                        'nomen' => $this->input->post('en_cat_name'),
                        'nomes' => $this->input->post('spa_cat_name'),
                        'nomal' => $this->input->post('ger_cat_name'),
                        'nomho' => $this->input->post('du_cat_name'),
                        'parent' => $this->input->post('parent'),
                        'img' => $upload_info['file_name']
                    );
                    $this->Product_m->add_category($data);
                }
                redirect('product/liste_categorie');
            } else {
                $this->display_view('product/new_categorie');
            }
        } else {
            $this->display_view('product/new_categorie');
        }
    }

    function delete_category($id, $per_page, $off_set) {
        is_admin();
        $query = $this->Product_m->get_children($id);
        if (count($query) > 0) {
            foreach ($query as $row) {
                $image_object = $this->Product_m->get_cat_image($row['id']);
                if ($this->Product_m->delete_category($row['id']) > 0) {
                    if ($image_object->num_rows() > 0) {
                        $info = $image_object->row();
                        $image_to_delete = './assets/catimg/' . $info->img;
                        $thumb_to_delete = './assets/catimg/thumbs/' . $info->img;
                        delete_image($image_to_delete, $thumb_to_delete);
                    }
                }
            }
        }
        $image_object = $this->Product_m->get_cat_image($id);
        if ($image_object->num_rows() > 0) {
            $info = $image_object->row();
            if ($info->img != null) {
                $image_to_delete = './assets/catimg/' . $info->img;
                $thumb_to_delete = './assets/catimg/thumbs/' . $info->img;
                delete_image($image_to_delete, $thumb_to_delete);
            }
        }
        $this->Product_m->delete_category($id);
        redirect('product/liste_categorie/' . $per_page . '/' . $off_set);
    }

    function update_category($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Catégorie d'animaux à jour";
        $config = array(
            array(
                'field' => 'fr_cat_name',
                'label' => 'Nom',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->_data['query'] = $this->Product_m->get_cat_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/catimg';
                $thumb_path = './assets/catimg/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 4000
                );
                $this->load->library('upload', $config);
                $this->_data['page_title'] = "Uploading";
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'nomfr' => $this->input->post('fr_cat_name'),
                        'nomen' => $this->input->post('en_cat_name'),
                        'nomes' => $this->input->post('spa_cat_name'),
                        'nomal' => $this->input->post('ger_cat_name'),
                        'nomho' => $this->input->post('du_cat_name'),
                        'parent' => $this->input->post('parent')
                    );
                    $this->Product_m->update_categorie($data,$id);
                } else {
                    $query = $this->Product_m->get_cat_by_id($id);
                    if ($query->num_rows() > 0) {
                        $info = $query->row();
                        $image_to_delete = './assets/catimg/' . $info->img;
                        $thumb_to_delete = './assets/catimg/thumbs/' . $info->img;
                        if($info->img!=NULL){
                            delete_image($image_to_delete, $thumb_to_delete);
                        }
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nomfr' => $this->input->post('fr_cat_name'),
                        'nomen' => $this->input->post('en_cat_name'),
                        'nomes' => $this->input->post('spa_cat_name'),
                        'nomal' => $this->input->post('ger_cat_name'),
                        'nomho' => $this->input->post('du_cat_name'),
                        'parent' => $this->input->post('parent'),
                        'img' => $upload_info['file_name']
                    );
                    $this->Product_m->update_categorie($data,$id);
                }
                redirect('product/liste_categorie');
            } else {
                $this->display_view('product/update_categorie');
            }
        } else {
            $this->display_view('product/update_categorie');
        }
    }

}

?>
